package com.galaxy.device.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.core.exception.ServiceException;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.device.domain.DeviceBase;
import com.galaxy.device.domain.DeviceStatus;
import com.galaxy.device.domain.bo.DeviceStatusBo;
import com.galaxy.device.domain.convert.DeviceStatusConvert;
import com.galaxy.device.domain.vo.DeviceStatusVo;
import com.galaxy.device.mapper.DeviceBaseMapper;
import com.galaxy.device.mapper.DeviceStatusMapper;
import com.galaxy.device.service.IDeviceStatusService;
import com.galaxy.device.service.InfluxDBService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备状态服务实现类
 */
@RequiredArgsConstructor
@Service
public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, DeviceStatus> implements IDeviceStatusService {

    private final DeviceStatusMapper deviceStatusMapper;
    private final DeviceBaseMapper deviceBaseMapper;
    private final InfluxDBService influxDBService;
    @DS("slave")
    @Override
    public List<DeviceStatusVo> queryList(DeviceStatusBo bo) {
        LambdaQueryWrapper<DeviceStatus> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getDeviceId() != null, DeviceStatus::getDeviceId, bo.getDeviceId());
        List<DeviceStatus> list = deviceStatusMapper.selectList(lqw);
        return DeviceStatusConvert.INSTANCE.convertVoList(list);
    }


//    @DS("slave")
//    public List<DeviceStatus> queryList(String deviceNo, String time) {
//        return influxDBService.queryDeviceStatusByConditions(deviceNo, time);
//    }


    @Override
    public DeviceStatusVo getInfo(Long id) {
        DeviceStatus status = deviceStatusMapper.selectById(id);
        if (status == null) {
            throw new ServiceException("设备状态信息不存在");
        }
        return DeviceStatusConvert.INSTANCE.convertVo(status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DeviceStatusBo bo) {
        DeviceStatus status = DeviceStatusConvert.INSTANCE.convert(bo);
        influxDBService.uploadedDeviceStatus(status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DeviceStatusBo bo) {
        DeviceStatus status = deviceStatusMapper.selectById(bo.getId());
        if (status == null) {
            throw new ServiceException("设备状态信息不存在");
        }
        MapstructUtils.convert(bo, status);
        deviceStatusMapper.updateById(status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByDeviceNo(DeviceStatusBo bo) {
        DeviceBase base = deviceBaseMapper.selectOne(new LambdaQueryWrapper<DeviceBase>().eq(DeviceBase::getDeviceNo, bo.getDeviceNo()));
        DeviceStatus status = deviceStatusMapper.selectOne(new LambdaQueryWrapper<DeviceStatus>().eq(DeviceStatus::getDeviceId, base.getId()));
        if (status == null) {
            throw new ServiceException("设备状态信息不存在");
        }
        bo.setId(status.getId());
        MapstructUtils.convert(bo, status);
        deviceStatusMapper.updateById(status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        deviceStatusMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deviceStatusMapper.deleteById(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOnline(Long deviceId) {
        DeviceStatus status = deviceStatusMapper.selectById(deviceId);
        if (status == null) {
            throw new ServiceException("设备状态信息不存在");
        }
        status.setLastOnlineTime(LocalDateTime.now());
        deviceStatusMapper.updateById(status);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOnlineByDeviceNo(String deviceNo) {
        DeviceBase base = deviceBaseMapper.selectOne(new LambdaQueryWrapper<DeviceBase>().eq(DeviceBase::getDeviceNo, deviceNo));
        DeviceStatus status = deviceStatusMapper.selectOne(new LambdaQueryWrapper<DeviceStatus>().eq(DeviceStatus::getDeviceId, base.getId()));
        if (status == null) {
            throw new ServiceException("设备状态信息不存在");
        }
        status.setLastOnlineTime(LocalDateTime.now());

        deviceStatusMapper.updateById(status);
    }

}
